package com.fight.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * @Title: AnalyticalURL.java
 * @Package com.sq.shared.util
 * @Description: TODO(解析URL)
 * @author: 阮启伟
 * @date: 2015年7月21日 下午4:20:56
 * @company: 上海航盛实业有限公司
 * @version V1.0
 */
public class AnalyticalURL {
	/**
	 * @Title: getStauts
	 * @Description: TODO(解析URL返回字符串)
	 * @param @param data
	 * @param @param method
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @author: 阮启伟
	 * @date 2015年7月21日 下午4:21:52
	 * @throws
	 */
	public static String getStauts(String urldata,String data, String method) {
		String result = "";
		URL url = null;
		BufferedReader read = null;
		HttpURLConnection conn = null;
		try {
			if (method.equals("GET")) {
				url = new URL(urldata+"?"+data);
				System.out.println("url="+url);
				conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod(method);// 提交模式
				// 定义 BufferedReader输入流来读取URL的响应
				read = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
				String line;// 循环读取
				while ((line = read.readLine()) != null) {
					result += line;
				}
				System.out.println(result+"==状态");
				read.close();
				conn.disconnect();
			}
			if (method.equals("POST")) {
				 URL url2 = new URL(urldata);
		            URLConnection con = url2.openConnection();
		            System.out.println("Received a : " + con.getClass().getName());
		            con.setDoInput(true);
		            con.setDoOutput(true);
		            con.setUseCaches(false);
		            con.setRequestProperty("CONTENT_LENGTH", "" + data.length()); 
		            OutputStream os = con.getOutputStream();
		            OutputStreamWriter osw = new OutputStreamWriter(os);
		            osw.write(data);
		            osw.flush();
		            osw.close();
		            System.out.println("After flushing output stream. ");
		            System.out.println("Getting an input stream...");
		            InputStream is = con.getInputStream();
		            // any response?
		            InputStreamReader isr = new InputStreamReader(is);
		            BufferedReader br = new BufferedReader(isr);
		            String line = null;
		            while ( (line = br.readLine()) != null)
		            {
		            	result += line;
		                System.out.println("line: " + line);
		            }
		        } }catch(Exception e){
		        	e.printStackTrace();
		        }
		return result;
	}

	/**
	 * @Title: getStauts
	 * @Description: TODO(httpClient Post解析URL返回字符串)
	 * @param @param data
	 * @param @param method
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @author: 阮启伟
	 * @date 2015年7月21日 下午4:21:52
	 * @throws
	 */
	public static String getStauts(String url,List<NameValuePair> nameValuePairs) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		UrlEncodedFormEntity encodedFormEntity = null;
		String result = "";
		try {
			encodedFormEntity = new UrlEncodedFormEntity(nameValuePairs, "utf-8");
			httppost.setEntity(encodedFormEntity);
			HttpResponse response = httpClient.execute(httppost);
			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			httpClient.getConnectionManager().shutdown();
		}
		return result ;
	}
	
	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		// 创建一个新分配的 byte 数组。
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;
	}
}
